<template>
    <div id="add-edit-role">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="769px" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="auto" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('9qhEbXnNbcKb3ZezfSpr1')" prop="name">
                            <el-input v-model="ruleForm.name" :placeholder="$t('wyHlUbe7qBuUytd321Bxm')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('oxaY6Iq4ENkeR9fv9r3Tl')" prop="code">
                            <el-input v-model="ruleForm.code" :placeholder="$t('lGiXTn1829PiF0yrT8E8')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('fBzbMi6UmAdXw0dU4FkW')" prop="sort">
                            <el-input v-model="ruleForm.sort" :placeholder="$t('z1O2gGdCnPppBkKhYfnMy')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('jJqL1X8ChQgp81e0Gpzm')" prop="remark">
                            <el-input type="textarea" v-model="ruleForm.remark" :placeholder="$t('98wJzI3lyj0Hk53nqIMpg')" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">{{ $t('gJikJmcygxvlVId7NvzSu') }}</el-button>
                <el-button type="primary" :disabled="repeatDis" @click="submitForm(ruleFormRef)">{{ $t('hfPwhz0wXrlEiXoAqXs9W') }}</el-button>
            </template>
        </el-dialog>
        
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue'
import { FormInstance, FormRules , ElMessage, ElButton, ElCol, ElDatePicker, ElDialog, ElDivider, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import { sysPosAdd , sysPosEdit } from '/@/api/manage/manage.ts'

/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    id: Number | String, // id
    name: String, // 角色名
    code: String | number, // 唯一编码
    sort: Number, // 排序
    remark: String, // 备注 
}
export default defineComponent( {
    name: 'add-edit-role',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            name: [
				{ required: true, message: window.$t('51ya2Io1HJRmdUhopku6'), trigger: 'blur' },
			],
            code: [
				{ required: true, message: window.$t('lGiXTn1829PiF0yrT8E8'), trigger: 'blur' },
			],
        })

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            ctx.emit('cancelUseClick')
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    const setObj = toRaw(state.ruleForm)
                    if(setObj.id === ''){
                        const {id , ...param} = setObj
                        let res = await sysPosAdd(param)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick')
                        }
                    }else{
                        let res = await sysPosEdit(setObj)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick')
                        }
                    }
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                state.repeatDis = false
                if(props.repairInfo !== ''){
                    titleCon.value = window.$t('znBGvEooAs8DutuukfBWt')
                    state.ruleForm = JSON.parse(props.repairInfo)
                }else{
                    titleCon.value = window.$t('6topq1R5EyLnD2mw79wXh')
                    state.ruleForm = {
                        id: '',
                        name: '', // 角色名
                        code: '', // 唯一编码
                        sort: 100, // 排序
                        remark: '', // 备注 
                    }
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
        }
    },
} )
</script>
<style scoped lang="scss">
</style>